package totask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import totask.dao.ProjectMapper;
import totask.dao.TaskMapper;
import totask.pojo.TotaskProject;
import totask.service.ProjectService;

import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    TaskMapper taskMapper;

    @Override
    public List<TotaskProject> queryProject(String CreateNameId) {
        QueryWrapper<TotaskProject> query = new QueryWrapper();
        query.eq("createnameid", CreateNameId);
        List<TotaskProject> totaskProjects = projectMapper.selectList(query);
        return totaskProjects;
    }

    @Override
    public String insertProject(TotaskProject totaskProject) {
        int insertProjectFlag = projectMapper.insert(totaskProject);
        if (insertProjectFlag > 0) {
            return "新增成功";
        } else {
            return "新增失败";
        }
    }

    @Override
    public String deleteProject(Integer projectId) {
        //删除项目
        QueryWrapper query = new QueryWrapper();
        query.eq("project_id",projectId);
        int deleteProjectFlag = projectMapper.delete(query);
        //删除任务project_id
        int deleteTaskFlag = taskMapper.delete(query);

        if (deleteProjectFlag > 0 && deleteTaskFlag>0) {
            return "项目已清空";
        } else {
            return "项目清空失败";
        }
    }

    @Override
    public String updateProject(Integer projectId,String createnameid) {
        TotaskProject totaskProject = new TotaskProject();
        totaskProject.setProjectId(projectId);
        totaskProject.setCreatenameid(createnameid);
        int updateById = projectMapper.updateById(totaskProject);
        if (updateById > 0) {
            return "移交成功";
        } else {
            return "移交失败";
        }
    }

    @Override
    public TotaskProject queryUserProjectAll(Integer projectId) {
        QueryWrapper<TotaskProject> query = new QueryWrapper();
        query.eq("project_id", projectId);
        return projectMapper.selectOne(query);
    }
}
